

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;

import butines.core.GameAction;
import butines.core.GameCore;
import butines.core.GameCursor;
import butines.core.GameImage;
import butines.core.InputCodes;
import butines.core.gui.GUIImage;
import butines.core.gui.GUIListener;
import butines.core.gui.GUIPainel;
import butines.core.gui.MouseEvent;
import butines.core.gui.style.Box;
import butines.core.gui.style.HAlign;
import butines.core.gui.style.Text;
import butines.core.gui.style.VAlign;

public class Main extends GameCore implements GUIListener {

    public static void main(String[] args) {
        new Main().startup();
    }

    private GameAction quit = new GameAction("Sair", GameAction.DETECT_INITAL_PRESS_ONLY);
    private GameAction toggle = new GameAction("toggle", GameAction.DETECT_INITAL_PRESS_ONLY);
    private GameAction play = new GameAction("play");
    private GameAction fullscreen = new GameAction("fullscreen", GameAction.DETECT_INITAL_PRESS_ONLY);
    private GameAction switchSize = new GameAction("switch size", GameAction.DETECT_INITAL_PRESS_ONLY);

    GUIImage guiNew;
    GUIImage guiQuit;
    
    @Override
    public void init() {
        // TODO Auto-generated method stub

        defineActionKey(quit, InputCodes.KEY_ESC);
        defineActionKey(toggle, InputCodes.KEY_I);
        defineActionKey(play, InputCodes.KEY_SPACE);
        defineActionKey(fullscreen, InputCodes.KEY_F1);
        defineActionKey(switchSize, InputCodes.KEY_F2);

        setWindow(640, 480);
        setRelativeMouseMode(false);
        setSystemCursorVisible(true);
        setFrameRate(30);
        setGameCursor(new GameCursor(new GameImage("datas/ui/cursor_arrow.png")));

        
        
        guiNew = new GUIImage(30, 30);
        
        guiQuit = new GUIImage(30, 70);
        
        createImageButton(guiNew, "Click Me", 112, 28);
        createImageButton(guiQuit, "Quit", 112, 28);
        
        GUIPainel menu = new GUIPainel();
        menu.add(guiNew);
        menu.add(guiQuit);
        
        addPainel(menu);
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    @Override
    public void update(float tpf) {

        if (quit.isPressed()) {
            shutdown();
        }
        if (toggle.isPressed()) {
            setRelativeMouseMode(!isRelativeMouseMode());
        }
        if (play.isPressed()) {
            playSound("datas/voice_select_3.wav", true);
        }
        if (fullscreen.isPressed()) {
            if (isFullScreen()) {
                setWindow(getWidth(), getHeight());
            } else {
                setFullScreen(getWidth(), getHeight());
            }
        }
        if (switchSize.isPressed()) {
            if (isFullScreen()) {
                if (getWidth() == 800) {
                    setFullScreen(640, 480);
                } else {
                    setFullScreen(800, 600);
                }
            } else {
                if (getWidth() == 800) {
                    setWindow(640, 480);
                } else {
                    setWindow(800, 600);
                }
            }
        }
    }

    @Override
    public void render(Graphics2D g2d) {
        g2d.setBackground(Color.white);
        g2d.clearRect(0, 0, getWidth(), getHeight());

    }
    
    
    
    public void createImageButton(GUIImage guiImage, String str, int w, int h) {
        
        Box box = new Box ();
        box.round(32, new Color(73, 19, 18), new Color(255,255,255, 25));
        box.setBackground(new Color(196, 51, 48), new Color(130, 32, 30));
        box.setSize(w, h);
        box.setCyclic(true);
        
        Box boxHover = box.copy();
        boxHover.setBackground(new Color(188, 49, 46), new Color(118, 29, 27));
        
        Box boxActive = box.copy();
        boxActive.round(32, new Color(73, 19, 18), new Color(0,0,0, 45));
        boxActive.setBackground(new Color(180, 42, 39), new Color(109, 25, 21));

        Box boxDisabled= box.copy();
        boxDisabled.setBackground(new Color(0x919191), new Color(0x353535));
        boxDisabled.round(32, new Color(73, 69, 68), new Color(255,255,255, 25));
        
        Text text = new Text(new Font("Arial", Font.BOLD, 12), HAlign.CENTER);
        text.setColor1(new Color(245, 245, 245));
        text.setColor2(new Color(245, 245, 245));
        text.setHAlign(HAlign.CENTER);
        text.setVAlign(VAlign.MIDDLE);
        
        Text shadow = text.copy();
        shadow.setColor1(Color.BLACK);
        shadow.setColor2(Color.BLACK);
        
        Text textHover = text.copy();
        textHover.setColor1(new Color(245, 245, 245));
        textHover.setColor2(new Color(245, 245, 245));
        
        Text textActive = text.copy();
        textActive.setColor1(new Color(255, 255, 255));
        textActive.setColor2(new Color(255, 255, 255));
        
        Text textDisabled = text.copy();
        textDisabled.setColor1(new Color(240, 240, 240));
        textDisabled.setColor2(new Color(240, 240, 240));
        
        Image background = GameImage.createCompatibleImage(w, h, Transparency.TRANSLUCENT); {
            Graphics2D g2d = (Graphics2D) background.getGraphics();
            
            box.draw(g2d, 0, 0);
            shadow.draw(g2d, 2, 2, str, w, h);
            text.draw(g2d, 0, 0, str, w, h);
        }
        
        Image hovered = GameImage.createCompatibleImage(w, h, Transparency.TRANSLUCENT); {
            Graphics2D g2d = (Graphics2D) hovered.getGraphics();
            
            boxHover.draw(g2d, 0, 0);
            
            shadow.draw(g2d, 2, 2, str, w, h);
            textHover.draw(g2d, 0, 0, str, w, h);
        }
        
        Image actived = GameImage.createCompatibleImage(w, h, Transparency.TRANSLUCENT); {
            Graphics2D g2d = (Graphics2D) actived.getGraphics();
            
            boxActive.draw(g2d, 0, 0);
            
            shadow.draw(g2d, 2, 3, str, w, h);
            textActive.draw(g2d, 0, 1, str, w, h);
            
        }
        
        Image disabled = GameImage.createCompatibleImage(w, h, Transparency.TRANSLUCENT); {
            Graphics2D g2d = (Graphics2D) disabled.getGraphics();
            
            boxDisabled.draw(g2d, 0, 0);
            shadow.draw(g2d, 2, 2, str, w, h);
            textDisabled.draw(g2d, 0, 0, str, w, h);
        }
        
        guiImage.setImage(new GameImage(background));
        guiImage.setImageActived(new GameImage(actived));
        guiImage.setImageHovered(new GameImage(hovered));
        guiImage.setImageDisabled(new GameImage(disabled));
        guiImage.setGuiListener(this);
    }

//    TextStyle ts = new TextStyle(); {
//        ts.setFont(new Font("Dialog", Font.BOLD, 16));
//        ts.getShadows().add(1, 1);
//        ts.getShadows().add(1, -1);
//        ts.getShadows().add(-1, 1);
//        ts.getShadows().add(-1, -1);
        
//    }
    
    
    Text text = new Text(new Font("Arial", Font.PLAIN, 32));
//    Text shadow= new Text(new Font("Arial", Font.PLAIN, 32), Color.WHITE, Color.BLACK, true);
    
    public void renderSystem(Graphics2D g2d) {
//        ts.drawString(g2d, 10, 2, "FPS " + getFps());
//        shadow.draw(g2d, 152, 152, "Chora\nViola\t!!!");
//        text.draw(g2d, 150, 150, "Chora  ~' }!^:;\"\'!|\nViola\t!!!\nPqp,CHORA VIOLA!!", 0);
    }

    @Override
    public void onClick(MouseEvent e) {
        System.out.println("click : " + e.getSource());
        if (e.getSource() == guiNew) {
            playSound("datas/voice_select_3.wav", true);
            guiNew.setEnabled(false);
        }
        
        if (e.getSource() == guiQuit) {
            shutdown();
        }
        
    }

    @Override
    public void onMouseEnter(MouseEvent e) {
        System.out.println("hover : " + e.getSource());
    }

    @Override
    public void onMouseLeave(MouseEvent e) {
        System.out.println("outer  : " + e.getSource());
    }

    @Override
    public void onMouseMove(MouseEvent e) {
        System.out.println("move   : " + e.getSource());
    }

    @Override
    public void onMouseDown(MouseEvent e) {
        System.out.println("onMouseDown  : " + e.getSource());
    }

    @Override
    public void onMouseUp(MouseEvent e) {
        System.out.println("onMouseUp    : " + e.getSource());
    }
    
/*
    
     class Menu extends GUIPainel {
      
      GUIBackground background;
      
      GUILabel fps;
      
      GUIButton newGame; GUIButton quitGame;
      
      public Menu() {
      
      background = new GUIBackground(0, 0, "datas/ui/Menu_background_with_title.png");
      
      fps = new GUILabel(0, 0, "FPS") { public void onUpdate() { text.setText("FPS " + getFps()); } public void
      onClick() { GradientColor color = new GradientColor(Color.RED, Color.YELLOW); fps.setColor(color);
      fps.setColorHovered(color); fps.setColorPressed(color); } };
      
      newGame = new GUIButton(270, 280, 150, 30, "New game") { public void onClick() {
      playSound("datas/voice_select_3.wav", 0, 0, true); setEnabled(false); } };
      
      quitGame = new GUIButton(270, 320, 150, 30, "Quit game") { public void onClick() { shutdown(); } };
      
      // add(background); // add(fps); // add(newGame); // add(quitGame); }
      
      }
 */
    
    
}
